<?php
uses('model');

class commentsModel extends Model {

    function __construct() {
        parent::__construct();
    }

    /*
    4.没有相应物品
    */
    function commentsList($v) {
        $this->dbconnect();

        //是用户评论?
        if ($v['uid']) {
            if ($v['pagesize']) {
                $data['list'] = $this->db->GetPage(array($this->table['comments'], '*', "uid={$v['uid']} ORDER BY ".$v['sort']." {$v['sortorder']}"), $data['page'], $v['pagesize']);
            } else {
                $data['list'] = $this->db->GetAll($this->table['comments'], '*', "uid={$v['uid']} ORDER BY ".$v['sort']." {$v['sortorder']}");
            }

            if ($data['list']) {
                foreach ($data['list'] as $vv) $tmp[]  = $vv['iid'];
                unset($vv);

                $itemslist = join(',', $tmp);

                $items = $this->db->GetAll($this->table['items'], 'id,name', "id IN($itemslist)");
                if ($items) foreach ($items as $vv) $data['items'][$vv['id']] = $vv;

                unset($vv);
                $comments = $this->db->GetAll($this->table['comments'], 'iid,MAX(updated) as updated,COUNT(*) AS ct', "iid IN($itemslist) AND uid<>{$v['uid']} GROUP BY iid");
                if ($comments) foreach ($comments as $vv) $data['comments'][$vv['iid']] = $vv;
            }

        } else {

            //所有评论
            if ($v['iid'] == 'all') {
                $data['list'] = $this->db->GetPage(array($this->table['comments']." a LEFT JOIN items b ON a.iid = b.id", 'a.*, b.name', "1 ORDER BY a.".$v['sort']." {$v['sortorder']}"), $data['page'], $v['pagesize']);
            } else {
                // TODO 重构
                //如果有 groupkey 则把同个 group 商品的评论一同展示
                if ($tmp = $this->db->GetOne($this->table['items'], 'groupkey', "id={$v['iid']}")) {
                    $groups = $this->db->cacheGetAll($this->table['items'], 'id,groupvalue', 'issale=1 AND groupkey='.$this->db->qstr($tmp));
                }

                if ($groups) {
                    foreach ($groups as $_v) $ids[] = $_v['id'];
                } else {
                    $ids[] = $v['iid'];
                }

                $ids = @join(',', $ids);

                if ($data['item'] = $this->db->GetRow($this->table['items'], 'id,name', "id={$v['iid']}")) {
                    if ($v['pagesize']) {
                        $data['list'] = $this->db->GetPage(array($this->table['comments'], '*', "iid IN($ids) ORDER BY ".$v['sort']." {$v['sortorder']}"), $data['page'], $v['pagesize']);
                    } else {
                        $data['list'] = $this->db->GetAll($this->table['comments'], '*', "iid IN($ids) ORDER BY ".$v['sort']." {$v['sortorder']}");
                    }
                } else {
                    $data['code']['commentsList'] = 4;
                }
            }
        }

        return $data;
    }

    function newComment($v) {
        $this->dbconnect();

        $fields['iid']      = $v['iid'];
        $fields['body']     = $v['body'];
        $fields['uid']      = $v['uid'];
        $fields['uname']    = $v['uname'];
        $fields['umail']    = $v['umail'];
        $fields['isadmin']  = $v['isadmin'];
        $fields['updated']  = time();

        $fields['ip']       = $v['ip'];

        //TrackBack
        if ($v['url']) {
            $fields['istb']     = 1;
            $fields['title']    = $v['title'];
            $fields['url']      = $v['url'];
            $fields['body']     = $v['excerpt'];
            $fields['uname']    = $v['blog_name'];
        }

        //检查物品是否存在
        if ($this->db->Count($this->table['items'], "id={$v['iid']}")) {
            //防止重复评论
            if ($this->db->Count($this->table['comments'], "iid={$v['iid']} AND uname=".$this->db->qstr($fields['uname'])." AND body=".$this->db->qstr($fields['body'])) == 0) {
                $data['code']['newComment'] = $this->db->Insert($this->table['comments'], $fields) ? 1 : 2;
            } else {
                $data['code']['newComment'] = 4;
            }
        } else {
            $data['code']['newComment'] = 64;
        }

        return $data;
    }

    function delComment($v) {
        $this->dbconnect();

        return $data['code']['delComment'] = $this->db->Delete($this->table['comments'], "id={$v['remove']}") ? 1 : 2;
    }
}
?>
